---
title: Dynamic Instructions Based on Session State
---

This example demonstrates how to create dynamic instructions that change based on session state, allowing personalized agent behavior for different users.

## Code

```python dynamic_instructions.py
from agno.agent import Agent
from agno.run import RunContext

def get_instructions(run_context: RunContext):
    if not run_context.session_state:
        run_context.session_state = {}

    if run_context.session_state.get("current_user_id"):
        return f"Make the story about {run_context.session_state.get('current_user_id')}."

    return "Make the story about the user."


agent = Agent(instructions=get_instructions)
agent.print_response("Write a 2 sentence story", user_id="john.doe")
```

## Usage

<Steps>
  <Snippet file="create-venv-step.mdx" />

  <Step title="Install libraries">
    ```bash
    pip install -U agno
    ```
  </Step>

  <Step title="Export your OpenAI API key">

    <CodeGroup>

    ```bash Mac/Linux
      export OPENAI_API_KEY="your_openai_api_key_here"
    ```

    ```bash Windows
      $Env:OPENAI_API_KEY="your_openai_api_key_here"
    ```
    </CodeGroup>
  </Step>

  <Step title="Create a Python file">
    Create a Python file and add the above code.
    ```bash
    touch dynamic_instructions.py
    ```
  </Step>

  <Step title="Run Agent">
    <CodeGroup>
    ```bash Mac
    python dynamic_instructions.py
    ```

    ```bash Windows
    python dynamic_instructions.py
    ```
    </CodeGroup>
  </Step>
  <Step title="Find All Cookbooks">
    Explore all the available cookbooks in the Agno repository. Click the link below to view the code on GitHub:

    <Link href="https://github.com/agno-agi/agno/tree/main/cookbook/agents/context_management" target="_blank">
      Agno Cookbooks on GitHub
    </Link>
  </Step>
</Steps>